#include "gui/gui.h"
#include "utils.h"

#include <windows.h>

#ifdef CLE_NAMESPACE
namespace utils {
#endif

GPoint  getPosFromRect(const GRect& rc, const GPoint& pt, const GSize& cell)
{
	int x = (pt.x - rc.left) / cell.w;
	int y = (pt.y - rc.top) / cell.h;
	return GPoint(x,y);
}

bool  isOperator(int ch)
{
	switch (ch)
	{
	case '%' : case '^' : case '&' : case '*' :
	case '(' : case ')' : case '-' : case '+' :
	case '=' : case '|' : case '{' : case '}' :
	case '[' : case ']' : case ':' : case ';' :
	case '<' : case '>' : case ',' : case '/' :
	case '?' : case '!' : case '.' : case '~' :
		return true;
	}
	return false;
}

inline char  upperCase(char c)
{
	if (c < 'a' || c > 'z') return c;
	return c - 'a' + 'A';
}

inline char  lowerCase(char c)
{
	if (c < 'A' || c > 'Z') return c;
	return c - 'A' + 'a';
}

void reverseString(char* s, int len)
{
    int i,j;
    for ( i = 0 , j = len - 1; i < j ; i++, j--)
    {
        char c = s[i];
        s[i] = s[j];
        s[j] =c;
    }
}
int num2string(int num, char *buffer)
{
    int len = 0;
    char* p = buffer;
    if (num == 0){
        *p++='0';
        *p=0;
        return 1;
    }
    if (num < 0)
    {
        num = - num;
        *p++ = '-';
    }
    while(num > 0){
        p[len++] = num % 10 + '0';
        num /= 10;
    }
    p[len] = 0;
    reverseString(p, len);
    return p + len - buffer;
}

int string2num(const char* str, int len)
{
    const char*p = str;
    if (*str == '-') p++;
    int num = 0;
    while(p - str < len && *p)
    {
        num = num * 10 + *p++ - '0';
    }
    if (*str == '-') num = -num;
    return num;
}

const char* next_character(const char* str, int encoding )
{
    const char* p = str;
    p = CharNextA(str);
    return p;
}

const char* last_character(const char* phead, const char* pcur ,int encoding )
{
    return CharPrevA(phead, pcur);
}

int ucs2_ascii(const wchar_t* str, int len, char* buf)
{
    int mlen = WideCharToMultiByte(0, 0, str, len, buf, 1024,0, 0);
    return mlen;
}

#ifdef CLE_NAMESPACE
}
#endif